local dinghan = fk.CreateSkill {

  name = "joy__dinghan",

  tags = {  },

}



dinghan:addEffect(fk.TargetConfirming, {
  name = "joy__dinghan",
  anim_type = "defensive",
  events = {fk.TargetConfirming, fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(dinghan.name) then
      return false
    end
    if event == fk.TargetConfirming then
      return
        data.card.type == Card.TypeTrick and
        data.card.name ~= "raid_and_frontal_attack" and
        not table.contains(type(player:getMark("@$joy__dinghan")) == "table" and player:getMark("@$joy__dinghan") or {}, data.card.trueName)
    else
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TurnStart then
      local room = player.room
      local dinghanRecord = type(player:getMark("@$joy__dinghan")) == "table" and player:getMark("@$joy__dinghan") or {}
      local allTricksName = {}
      for _, id in ipairs(Fk:getAllCardIds()) do
        local card = Fk:getCardById(id)
        if card.type == Card.TypeTrick and not card.is_derived and not table.contains(dinghanRecord, card.trueName) then
          table.insertIfNeed(allTricksName, card.trueName)
        end
      end
      local choices = {"Cancel"}
      if #allTricksName > 0 then
        table.insert(choices, 1, "joy__dinghan_addRecord")
      end
      if #dinghanRecord > 0 then
        table.insert(choices, 2, "joy__dinghan_removeRecord")
      end
      local choice = room:askForChoice(player, choices, dinghan.name)
      if choice == "Cancel" then
        return false
      end
      local cardName = room:askForChoice(player, choice == "joy__dinghan_addRecord" and allTricksName or dinghanRecord, dinghan.name)
      event:setCostData(self, { choice = choice, cardName = cardName })
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dinghanRecord = type(player:getMark("@$joy__dinghan")) == "table" and player:getMark("@$joy__dinghan") or {}
    if event == fk.TargetConfirming then
      table.insert(dinghanRecord, data.card.trueName)
      room:setPlayerMark(player, "@$joy__dinghan", dinghanRecord)
      data:cancelTarget(player.id)
      return true
    else
      local costData = event:getCostData(self)
      if costData.choice == "joy__dinghan_addRecord" then
        table.insert(dinghanRecord, costData.cardName)
      else
        table.removeOne(dinghanRecord, costData.cardName)
      end
      room:setPlayerMark(player, "@$joy__dinghan", #dinghanRecord > 0 and dinghanRecord or 0)
    end
  end,
})
dinghan:addEffect(fk.TurnStart, {
  name = "joy__dinghan",
  anim_type = "defensive",
  events = {fk.TargetConfirming, fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(dinghan.name) then
      return false
    end
    if event == fk.TargetConfirming then
      return
        data.card.type == Card.TypeTrick and
        data.card.name ~= "raid_and_frontal_attack" and
        not table.contains(type(player:getMark("@$joy__dinghan")) == "table" and player:getMark("@$joy__dinghan") or {}, data.card.trueName)
    else
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TurnStart then
      local room = player.room
      local dinghanRecord = type(player:getMark("@$joy__dinghan")) == "table" and player:getMark("@$joy__dinghan") or {}
      local allTricksName = {}
      for _, id in ipairs(Fk:getAllCardIds()) do
        local card = Fk:getCardById(id)
        if card.type == Card.TypeTrick and not card.is_derived and not table.contains(dinghanRecord, card.trueName) then
          table.insertIfNeed(allTricksName, card.trueName)
        end
      end
      local choices = {"Cancel"}
      if #allTricksName > 0 then
        table.insert(choices, 1, "joy__dinghan_addRecord")
      end
      if #dinghanRecord > 0 then
        table.insert(choices, 2, "joy__dinghan_removeRecord")
      end
      local choice = room:askForChoice(player, choices, dinghan.name)
      if choice == "Cancel" then
        return false
      end
      local cardName = room:askForChoice(player, choice == "joy__dinghan_addRecord" and allTricksName or dinghanRecord, dinghan.name)
      event:setCostData(self, { choice = choice, cardName = cardName })
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dinghanRecord = type(player:getMark("@$joy__dinghan")) == "table" and player:getMark("@$joy__dinghan") or {}
    if event == fk.TargetConfirming then
      table.insert(dinghanRecord, data.card.trueName)
      room:setPlayerMark(player, "@$joy__dinghan", dinghanRecord)
      data:cancelTarget(player.id)
      return true
    else
      local costData = event:getCostData(self)
      if costData.choice == "joy__dinghan_addRecord" then
        table.insert(dinghanRecord, costData.cardName)
      else
        table.removeOne(dinghanRecord, costData.cardName)
      end
      room:setPlayerMark(player, "@$joy__dinghan", #dinghanRecord > 0 and dinghanRecord or 0)
    end
  end,
})

return dinghan